package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.entity.Project;
import com.pms.util.DBHelp;
import com.pms.util.RowMapper;

/**
 * 2013-03-10
 * @author handy
 *
 */
public class ProjectDao {
	
	private DBHelp<Project> db = new DBHelp<Project>();
	
	public List<Project> findRole(String userid){
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT tp.proid, tp.proname, tp.desc, tp.procreatetime FROM t_user_pro AS tup, t_project AS tp ");
		sb.append("WHERE tup.`t_project_id` = tp.proid AND tup.`role` = '1' AND tup.t_user_useid = ?");
		return db.executeQueryToList(sb.toString(), new ProRowMapper(), userid);
	}
	public void save(Project pro){
		String sql="INSERT INTO t_project(proid, proname,`desc`) VALUE(?,?,?)";
		db.executeSQL(sql,pro.getPro_id(), pro.getProname(),pro.getPro_context());
	}
	
	public void update(Project pro){
		String sql = "update t_project set proname = ?, desc = ? where proid = ?";
		db.executeSQL(sql, pro.getProname(), pro.getPro_context(), pro.getPro_id());
	}
	
	public Project findById(String id){
		String sql = "select proid, proname, `desc`, procreatetime from t_project where proid = ?";
		return db.executeQueryToObject(sql, new ProRowMapper(), id);
	}
	
	public List<Project> findAllProject(){
		String sql = "SELECT proid, proname, `desc`, procreatetime FROM t_project";
		return db.executeQueryToList(sql, new ProRowMapper());
	}
	
	public int count(){
		String sql = "SELECT COUNT(*) FROM t_project";
		return db.executeQueryToNumber(sql).intValue();
	}
	
	private class ProRowMapper implements RowMapper<Project>{

		@Override
		public Project mapRow(ResultSet rs) throws SQLException {

			Project pro = new Project();
			pro.setPro_id(rs.getString("proid"));
			pro.setProname(rs.getString("proname"));
			pro.setPro_context(rs.getString("desc"));
			pro.setCreatetime(rs.getString("procreatetime"));
			return pro;
		}
		
	}
}
